ABC250 C - Adjacent Swaps
https://atcoder.jp/contests/abc250/tasks/abc250_c
提出
code: python
n, q = map(int, input().split())
x = int(input()) for _ in range(q)
ans = i for i in range(1, n+1)
# 1 5 2 9 6 6
# 1 2 3 4 5 6 7 8 9 10
# 2 1 3 4 5 6 7 8 9 10
# 2 1 3 4 6 5 7 8 9 10
# 1 2 3 4 6 5 7 8 9 10
# 1 2 3 4 6 5 7 8 10 9
# 1 2 3 4 5 6 7 8 10 9
# 1 2 3 4 5 7 6 8 10 9
# 右側 or 左側と入れ替え = +1 or -1
for i in x:
if (ans-1 == i):
if (ans-1 > ans-2):
ans-1 -= 1
ans-2 += 1
else:
ans-1 += 1
ans-2 -= 1
else:
target = ans.index(i)
if (anstarget > anstarget+1):
anstarget -= 1
anstarget+1 += 1
else:
anstarget += 1
anstarget+1 -= 1
print(*ans, sep=" ")
解答
code: python
n, q = map(int, input().split())
x = int(input()) for _ in range(q)
ans = i for i in range(n+1)
# 整数 j が書かれているボールが左から何番目かを表す変数 posj
pos = i for i in range(n+1)
# ans: 0, 1, 2, 3, 4, 5
# pos: 0, 1, 2, 3, 4, 5
for v in x:
# print(ans == pos) True
p0 = posv
p1 = p0
if (p0 != n):
p1 += 1 # 右隣
else:
p1 -= 1 # 左隣
ansp0, ansp1 = ansp1, ansp0
# pos を ans に追従させる
v0 = ansp0
v1 = ansp1
posv0, posv1 = posv1, posv0
ans.pop(0)
print(*ans, sep=" ")
メモ
https://atcoder.jp/contests/abc250/editorial/3905
提出
code: python
n, q = map(int, input().split())
x = int(input()) for _ in range(q)
ball = i for i in range(1, n+1)
# O(NQ)
for v in x:
idx = ball.index(v)
if idx != n-1:
ballidx, ballidx+1 = ballidx+1, ballidx
else:
ballidx, ballidx-1 = ballidx-1, ballidx
for v in ball:
print(v, end=" ")